
// import { find , confirm } from '../../../../api/point'
import {find as findMember} from '../../../../api/member'
import { find , facePay} from '../../../../api/payment'
const util = require('../../../../utils/util.js')
Page({
  data: {
    id:'',
    tranSn:'',
    depositId:'',
    name:'',
    credit:'',
    debit:'',
    PaymentAmount:'0.00',
    payCode:'',
    sn:''
  },
  onLoad: function(options) {
    var _this = this;
    _this.data.id = options.id
    _this.setSaveParameters(_this.data.id)
  },
  priceFormat(value) {
    var toFixedNum = Number(value).toFixed(3);
    var realVal = toFixedNum.substring(0, toFixedNum.toString().length - 1);
    return realVal;
  },
  setSaveParameters(id){
    var _this = this
    find(id).then(res=>{
      console.log(res)
      _this.data.name = res.data.payment.memberName
      _this.data.PaymentAmount = res.data.payment.amount
      _this.data.sn = res.data.payment.sn
      if(res.data.payment.memberId){
        _this.findMemberClick(res.data.payment.memberId)
      }
      _this.setData({
        name: _this.data.name,
        PaymentAmount:_this.data.PaymentAmount,
        sn:_this.data.sn
      })
    })
  },
  metaClick(data) {
    this.data.tranSn = data.detail.images[0]
  },
  confirmClick() {
    
    if( this.data.tranSn == "" ){
      wx.showToast({
        title: '请先上传付款凭证',
        icon: 'none',
        duration: 2000
      })      
      return false
    }

    var data = {
      sn:this.data.sn,
      tranSn:this.data.tranSn
    }
    facePay(data).then(res=>{
      wx.showToast({
        title: "支付成功",
        icon: 'none',
        duration: 3000,
        success:function(){
          setTimeout(() => {
            wx.navigateBack({
                delta: 1
            })
          }, 1000)
        }
      })
    }).catch(error=>{
      wx.showToast({
        title: error.content,
        icon: 'none',
        duration: 3000,
      })
    })
  },
  
  previewClick(){
    if(this.data.payCode){
      var data = []
      data.push(this.data.payCode)
      wx.previewImage({
        current:  this.data.payCode,// 当前显示图片的http链接
        urls: data // 需要预览的图片http链接列表
      })
    }
  },
  findMemberClick(id){
    var _this = this
    _this.data.payCode = ''
    findMember(id).then(res=>{
      console.log(res)
      if(res.data.member.payCode){
        _this.data.payCode = res.data.member.payCode
        _this.setData({
          payCode:_this.data.payCode
        })
      }
    })
  }
})
